package control.control.usermanage;

import base.bean.StandardResponse;
import base.standard.constant.message.MessageCode;
import base.util.ResponseUtil;
import control.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;

/**
 * Login control
 *
 * @description: 登陆控制器
 * @author: cyj
 * @date 2020 -10-21 20:39:01
 */
@RestController
@RequestMapping("/login")
@ResponseStatus(code = HttpStatus.OK)
@Slf4j
public class LoginControl {

    @Autowired
    private IUserService userService;

    @GetMapping
    public ResponseEntity<StandardResponse> login(@NotNull(message = "loginId 不能为空") String loginId,
                                                  @NotNull(message = "password 不能为空") String password) {
        String token = userService.login(loginId, password);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("jwt-token", token);
        return new ResponseEntity(ResponseUtil.success(MessageCode.USER_LOGIN, true), responseHeaders,
                HttpStatus.OK);
    }

}
